package com.manage.controller;

import com.manage.pojo.Shoppings;
import com.manage.pojo.operation.ShoppingsList;
import com.manage.service.ShoppingsService;
import com.manage.utils.FBJSONResult;
import com.manage.utils.PagedResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@Api(value = "有关购物车的操作", tags = {"有关购物车的操作"})
public class ShoppingsController {

    @Autowired
    private ShoppingsService shoppingsService;

    @ApiOperation(value = "购物车添加", notes = "购物车添加接口")
    @PostMapping("/shopping/shoppingRegist")
    public FBJSONResult collectionRegist(@RequestBody Shoppings shopping) throws Exception {
        shopping.setUserId(shopping.getUserId());
        shopping.setShoesId(shopping.getShoesId());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        shopping.setCreateTime(sdf.format(new Date()));
        shoppingsService.saveShoppings(shopping);
        return FBJSONResult.ok(shopping);
    }

    @ApiOperation(value = "购物车删除", notes = "购物车删除接口")
    @ApiImplicitParam(name = "shoppingId", value = "购物车id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/shopping/shoppingDelete")
    public FBJSONResult shoppingDelete(String shoppingId) throws Exception {
        if(StringUtils.isBlank(shoppingId)) {
            return FBJSONResult.errorMsg("购物车id不能为空");
        }
        boolean shoppingIdIsExit = shoppingsService.queryShoppingsIsExit(shoppingId);
        if(shoppingIdIsExit) {
            shoppingsService.shoppingDelete(shoppingId);
        }
        return FBJSONResult.ok("删除成功");
    }

    @ApiOperation(value = "购物车信息查询(根据购物车id)", notes = "购物车信息查询(根据购物车id)接口")
    @ApiImplicitParam(name = "shoppingId", value = "购物车id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/shopping/queryShoppingInfoByShoppingId")
    public FBJSONResult queryShoppingInfoByShoppingId(String shoppingId) throws Exception {
        if(StringUtils.isBlank(shoppingId)) {
            return FBJSONResult.errorMsg("购物车id不能为空");
        }
        ShoppingsList shoppingResult = shoppingsService.queryShoppingsInfoByShoppingId(shoppingId);
        return FBJSONResult.ok(shoppingResult);
    }

    @ApiOperation(value = "购物车信息查询(根据用户id)", notes = "购物车信息查询(根据用户id)接口")
    @ApiImplicitParam(name = "userId", value = "用户id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/shopping/queryShoppingInfoByUserId")
    public FBJSONResult queryShoppingInfoByUserId(Integer page, String userId) throws Exception {
        if(StringUtils.isBlank(userId)) {
            return FBJSONResult.errorMsg("用户id不能为空");
        }
        if(page == null) {
            page =1;
        }
        PagedResult result = shoppingsService.queryShoppingsInfoByUserId(page, 10, userId);
        return FBJSONResult.ok(result);
    }

    @ApiOperation(value = "购物车信息查询(根据鞋id)", notes = "购物车信息查询(根据鞋id)接口")
    @ApiImplicitParam(name = "shoesId", value = "鞋id", required = true, dataType = "String", paramType = "query")
    @PostMapping("/shopping/queryShoppingInfoByShoesId")
    public FBJSONResult queryShoppingInfoByShoesId(Integer page, String shoesId) throws Exception {
        if(StringUtils.isBlank(shoesId)) {
            return FBJSONResult.errorMsg("鞋id不能为空");
        }
        if(page == null) {
            page =1;
        }
        PagedResult result = shoppingsService.queryShoppingsInfoByShoesId(page, 10, shoesId);
        return FBJSONResult.ok(result);
    }

    @ApiOperation(value = "购物车信息查询(所有)", notes = "购物车信息查询(所有)接口")
    @PostMapping("/shopping/queryAllShoppingInfo")
    public FBJSONResult queryAllShoppingInfo(Integer page) throws Exception {
        if(page == null) {
            page =1;
        }
        PagedResult result = shoppingsService.queryAllShoppings(page, 10);
        return FBJSONResult.ok(result);
    }

}
